package cn.power.win.win_power.acitivity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Resources;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


import java.util.ArrayList;
import java.util.List;

import cn.power.win.win_power.MyApp;
import cn.power.win.win_power.R;
import cn.power.win.win_power.utils.ASUtils;
import cn.power.win.win_power.utils.DeviceBean;
import cn.power.win.win_power.utils.SpUtils;
import cn.power.win.win_power.view.VerificationCodeView;
import me.jessyan.autosize.AutoSizeCompat;


/*
 * 手动输入界面
 * */
public class ManullyEnterActivity extends AppCompatActivity  {

    private VerificationCodeView mCodeView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manully_enter);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        //返回
        findViewById(R.id.iv_back2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mCodeView = (VerificationCodeView) findViewById(R.id.vcv);
        mCodeView.setOnCodeFinishListener(new VerificationCodeView.OnCodeFinishListener() {
            @Override
            public void onComplete(String content) {
               // Toast.makeText(ManullyEnterActivity.this, content, Toast.LENGTH_SHORT).show();
                ShowInputPassword(content);
                // 发送广播
                Intent intent = new Intent("cn.power.win.win_power");
                intent.putExtra("data", content);
                LocalBroadcastManager.getInstance(ManullyEnterActivity.this).sendBroadcast(intent);
                finish();

            }
        });

    }

    private void ShowInputPassword(String content) {
        View contentView = LayoutInflater.from(ManullyEnterActivity.this).inflate(R.layout.popupwindow_password, null);
        PopupWindow popupWindow = new PopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true);
        popupWindow.setContentView(contentView);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);// 设置允许在外点击消失
        //初始化界面
        final EditText etPassword = (EditText) contentView.findViewById(R.id.et_password);
        TextView tvTrue = (TextView) contentView.findViewById(R.id.tv_true);
        TextView tvCancel = (TextView) contentView.findViewById(R.id.tv_cancel);
        LinearLayout ll = (LinearLayout) contentView.findViewById(R.id.ll);

        ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        // 取消
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        // 确认
        tvTrue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!ASUtils.isCanUse(etPassword.getText().toString().trim())) {
                    Toast.makeText(ManullyEnterActivity.this,"请先输入密码",Toast.LENGTH_SHORT).show();
                }else {
                    List<String> mPassword = new ArrayList<>();
                    mPassword.add(etPassword.getText().toString().trim());
                    DeviceBean mDeviceBean = new DeviceBean(content,mPassword);
                    // 以mac地址为键值来存取对象
                    SpUtils.putObject(MyApp.context,mDeviceBean.getMac(),mDeviceBean);
                    //取消弹窗
                    popupWindow.dismiss();

                }

            }
        });
        ColorDrawable dw = new ColorDrawable(0x70000000);
        popupWindow.setBackgroundDrawable(dw);
        popupWindow.showAtLocation(contentView, Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0, 0);

    }



    @Override
    public Resources getResources() {
        //需要升级到 v1.1.2 及以上版本才能使用 AutoSizeCompat
     //   AutoSizeCompat.autoConvertDensityOfGlobal((super.getResources()));//如果没有自定义需求用这个方法
        AutoSizeCompat.autoConvertDensity((super.getResources()), 667, false);//如果有自定义需求就用这个方法
        return super.getResources();
    }
}
